package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoning.office_system.model.Equipment;
import com.guoning.office_system.model.EquipmentWithMeetingRoom;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.EquipmentService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/equipment")
public class EquipmentController extends BaseController {
    @Autowired
    EquipmentService equipmentService;

    /**
     * 条件查询（分页）
     * @param pageIndex
     * @param pageSize
     * @param equipmentCodeOrName
     * @param equipmentStatus
     * @return
     */
    @RequestMapping(value = "/select")
    public JsonResult select(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "roomId", defaultValue = "") Long roomId,
                             @RequestParam(value = "equipmentCodeOrName", defaultValue = "") String equipmentCodeOrName,
                             @RequestParam(value = "equipmentStatus", defaultValue = "") Integer equipmentStatus){
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("roomId", roomId);
            criteria.addExtField("equipmentCodeOrName", equipmentCodeOrName);
            criteria.addExtField("equipmentStatus", equipmentStatus);
            pageBean = equipmentService.selectByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 修改设备状态
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateEquipmentStatus", method = RequestMethod.POST)
    public JsonResult updateEquipmentStatus(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            Map param = JSONObject.parseObject(params, Map.class);
            Integer status = Integer.parseInt(param.get("status").toString());
            List<EquipmentWithMeetingRoom> list = JSONArray.parseArray(param.get("list").toString(), EquipmentWithMeetingRoom.class);
            User user = getUserDate(request);
            for(int i=0; i<list.size(); i++){
                list.get(i).setModifier(user.getUserName());
                list.get(i).setEquipmentStatus(status);
            }
            int i = equipmentService.updateEquipmentStatus(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 删除设备
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult updateForDelete(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            List<EquipmentWithMeetingRoom> list = JSONArray.parseArray(params, EquipmentWithMeetingRoom.class);
            User user = getUserDate(request);
            for(int i=0; i<list.size(); i++){
                list.get(i).setModifier(user.getUserName());
            }
            int i = equipmentService.updateForDelete(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 新增
     * @param request
     * @param equipment
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, Equipment equipment){
        JsonResult jsonResult;
        try {
            Long count = equipmentService.selectByEquipmentCodeCount(equipment.getEquipmentCode());
            if(count != 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "设备编码已存在，请检查");
                return jsonResult;
            }else {
                User user = getUserDate(request);
                equipment.setCreator(user.getUserName());
                Long i = equipmentService.insert(equipment);
                if(i == null){
                    jsonResult = new JsonResult(Constants.STATUS_FAIL, "新增失败");
                    return jsonResult;
                }
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "新增成功");
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "新增异常");
        }
        return jsonResult;
    }

    /**
     * 修改设备
     * @param request
     * @param equipment
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult update(HttpServletRequest request, Equipment equipment){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            equipment.setModifier(user.getUserName());
            equipment.setModifiedTime(new Date());
            int i = equipmentService.updateByPrimaryKeySelective(equipment);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "修改成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "修改异常");
        }
        return jsonResult;
    }
}
